<?php
/**
 * CsvExporter
 *
 * PHP version 5
 *
 * @category
 * @package
 * @author   Evgeny Utehin <utehin@ebola.com.ua>
 */

Yii::import('ext.csvexporter.components.workers.*');

class CsvExporter extends CApplicationComponent
{
    /**
     * Validate params & create needed class
     *
     * @param  CActiveDataProvider|CArrayDataProvider $dataProvider
     * @param  array  $includedColumns
     * $includedColumns example:
     * <code>
     * <?php
     *     $includedColumns = array(
     *         'column name' => array(
     *              'header'   => 'custom header',
     *              'relation' => 'name of relation',
     *          ),
     *     );
     * ?>
     * </code>
     * @example How to initiate includedColumns
     *
     * @param  array  $excludedColumns
     * $excludedColumns example:
     * <code>
     * <?php
     *     $excludedColumns = array(
     *         'column 1 name',
     *         'column 2 name',
     *         '...',
     *     );
     * ?>
     * </code>
     * @example How to initiate excludedColumns
     *
     * @throws Exception If dataProvider is not instanceof CActiveDataProvider|CArrayDataProvider
     *
     * @return CsvExporterActiveDataProvider|CsvExporterArrayDataProvider
     */
    public function buildReport($dataProvider, $includedColumns = array(),
        $excludedColumns = array('id', 'create_time', 'update_time'))
    {
        if ($dataProvider instanceof CActiveDataProvider) {
            return new CsvExporterActiveDataProvider($dataProvider, $includedColumns, $excludedColumns);
        } elseif($dataProvider instanceof CArrayDataProvider) {
            return new CsvExporterArrayDataProvider($dataProvider, $includedColumns, $excludedColumns);
        } else {
            throw new Exception(get_class($this) . ": DataProvider must be an "
                . "instance of CActiveDataProvider or CArrayDataProvider");
        }
    }
}
